package xxl.robotlegs.extensions.viewmanager 
{
	import xxl.robotlegs.extensions.viewmanager.api.IViewManager;
	import xxl.robotlegs.extensions.viewmanager.impl.ContainerRegistry;
	import xxl.robotlegs.extensions.viewmanager.impl.ViewManager;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.api.IExtension;
	import xxl.robotlegs.framework.api.IInjector;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class ViewManagerExtension implements IExtension 
	{
		private static var _containerRegistry:ContainerRegistry;
		
		private var _injector:IInjector;
		private var _viewManager:IViewManager;
		
		public function ViewManagerExtension() 
		{
			
		}
		
		public function toString():String
		{
			return "ViewManagerExtension（简介：提供一个统一管理所有显示元件的管理器IViewManager）";
		}
		
		private function whenInitializing():void
		{
			_viewManager = _injector.getInstance(IViewManager);
		}
		
		private function whenDestroying():void
		{
			_viewManager.removeAllHandlers();
			_injector.unmap(IViewManager);
			_injector.unmap(ContainerRegistry);
		}
		
		/* INTERFACE xxl.robotlegs.core.IExtension */
		
		public function extend(context:IContext):void 
		{
			context.whenInitializing(whenInitializing);
			context.whenDestroying(whenDestroying);
			
			_injector = context.injector;
			
			_containerRegistry ||= new ContainerRegistry();
			_injector.map(ContainerRegistry).toValue(_containerRegistry);
			_injector.map(IViewManager).toSingleton(ViewManager);
		}
		
	}

}